/*
    SUSAN® - Sole of Unix Save ANything

   Copyright (C) 2016-2019 Skyatlas co. ltd

   
*/
#ifndef BAREOS_LIB_BNET_H_
#define BAREOS_LIB_BNET_H_

#include "tls.h"

class dlist;

int32_t BnetRecv(BareosSocket* bsock);
bool BnetSend(BareosSocket* bsock);
bool BnetFsend(BareosSocket* bs, const char* fmt, ...);
bool BnetSetBufferSize(BareosSocket* bs, uint32_t size, int rw);
bool BnetSig(BareosSocket* bs, int sig);
bool BnetTlsServer(BareosSocket* bsock,
                   const std::vector<std::string>& verify_list);
bool BnetTlsClient(BareosSocket* bsock,
                   bool VerifyPeer,
                   const std::vector<std::string>& verify_list);
int BnetGetPeer(BareosSocket* bs, char* buf, socklen_t buflen);
BareosSocket* dup_bsock(BareosSocket* bsock);
const char* BnetStrerror(BareosSocket* bsock);
const char* BnetSigToAscii(BareosSocket* bsock);
std::string BnetSignalToString(int signal);
std::string BnetSignalToDescription(int signal);
int BnetWaitData(BareosSocket* bsock, int sec);
int BnetWaitDataIntr(BareosSocket* bsock, int sec);
bool IsBnetStop(BareosSocket* bsock);
int IsBnetError(BareosSocket* bsock);
void BnetSuppressErrorMessages(BareosSocket* bsock, bool flag);
dlist* BnetHost2IpAddrs(const char* host, int family, const char** errstr);
int BnetSetBlocking(BareosSocket* sock);
int BnetSetNonblocking(BareosSocket* sock);
void BnetRestoreBlocking(BareosSocket* sock, int flags);
int NetConnect(int port);
BareosSocket* BnetBind(int port);
BareosSocket* BnetAccept(BareosSocket* bsock, char* who);

enum : uint32_t
{
  kMessageIdUnknown = 0,
  kMessageIdProtokollError = 1,
  kMessageIdReceiveError = 2,
  kMessageIdOk = 1000,
  kMessageIdPamRequired = 1001,
  kMessageIdInfoMessage = 1002,
  kMessageIdPamInteractive = 4001,
  kMessageIdPamUserCredentials = 4002
};

class BStringList;

#ifdef BAREOS_TEST_LIB
bool ReadoutCommandIdFromMessage(const BStringList& list_of_arguments,
                                 uint32_t& id_out);
bool EvaluateResponseMessageId(const std::string& message,
                               uint32_t& id_out,
                               BStringList& args_out);
#endif

bool ReceiveAndEvaluateResponseMessage(BareosSocket* bsock,
                                       uint32_t& id_out,
                                       BStringList& args_out);
bool FormatAndSendResponseMessage(BareosSocket* bsock,
                                  uint32_t id,
                                  const std::string& str);
bool FormatAndSendResponseMessage(BareosSocket* bsock,
                                  uint32_t id,
                                  const BStringList& list_of_agruments);

#endif  // BAREOS_LIB_BNET_H_
